
import Sequelize from 'sequelize'
import sequelize from './../../../database'

const ManualOrderDetail = sequelize.define('original_detail',{
    id:{
        type: Sequelize.UUIDV4,
        defaultValue: Sequelize.UUIDV4,
        unique: true,
        allowNull: false,
        primaryKey:true
    },
    original_order_id:{
        type: Sequelize.UUIDV4,
        allowNull: false
    },
    internal_trade_id:{
        type: Sequelize.UUIDV4,
        allowNull: false
    },
    quantity:{
        type: Sequelize.DOUBLE,
        allowNull: false
    },
    unit_price:{
        type: Sequelize.DOUBLE
    },
    total_price:{
        type: Sequelize.DOUBLE
    },
    memo: {
        type: Sequelize.STRING
    },
    status: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        allowNull: false
    },
    created_man: {
        type: Sequelize.STRING,
        allowNull: false
    }
},{
    tableName: 'original_detail',
    updatedAt:'updated_at',
    createdAt:'created_at'
});

class ManualOrderDetailModel{
    insert(record){
        return ManualOrderDetail.bulkCreate(record)
    }
    delete(id){
        return ManualOrderDetail.destroy({id: id});
    }
    deleteByOrderId(order_id){
        return ManualOrderDetail.destroy({
            where:{
                original_order_id: order_id
            }
        })
    }
    async findByOrderId(order_id){
        return await sequelize.query(`select od.*, it.internal_trade_name, it.internal_trade_unit, ity.internal_type_name
        from
        original_detail od, internal_trade it, internal_type ity
        where
        od.id=${order_id} and it.id=od.internal_trade_id and ity.id=it.internal_type_id`, { type: sequelize.QueryTypes.SELECT })
    }
}
module.exports = new ManualOrderDetailModel();